package com.yunlong.softpark.controller;

import com.yunlong.softpark.common.enums.BaseErrorCodeEnum;
import com.yunlong.softpark.core.exception.SysException;
import com.yunlong.softpark.core.support.web.controller.BaseController;
import com.yunlong.softpark.core.wrapper.ResultWrapper;
import com.yunlong.softpark.dto.FeedbackDto;
import com.yunlong.softpark.dto.UserInfo;
import com.yunlong.softpark.form.FeedBackDataForm;
import com.yunlong.softpark.form.FeedbackForm;
import com.yunlong.softpark.redis.RedisRepository;
import com.yunlong.softpark.service.FeedbackService;
import com.yunlong.softpark.service.RecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @author Cui
 * @email ${email}
 * @date 2020-07-21 16:54:16
 */
@Api(value = "FeedbackController", tags = {"反馈接口"})
@RestController
@Slf4j
@RequestMapping("/feedback")
public class FeedbackController extends BaseController<UserInfo> {

    @Autowired
    FeedbackService feedbackService;

    @Autowired
    RedisRepository redisRepository;

    @Autowired
    RecordService recordService;

    /**
     * 向FeedBack表中添加一条记录
     *
     * @param feedbackForm
     * @return
     */
    @ApiOperation("添加对于软件的反馈")
    @RequestMapping(path = "/insert", method = RequestMethod.POST)
    public ResultWrapper insetFeedbackData(@Valid @RequestBody FeedbackForm feedbackForm,
                                    BindingResult bindingResult,
                                    @RequestHeader("ANSWER_ACCESS_TOKEN") String token,
                                    HttpServletRequest request) {
        try {
            validateParams(bindingResult);
            if (token != null){
                String userId = redisRepository.selectAccessToken(token);
                int feedbackId = feedbackService.insertFeedbackData(feedbackForm, userId);
                recordService.saveRecord(request.getRemoteAddr(),4,feedbackId+"",userId);
            }else {
                int feedbackId = feedbackService.insertFeedbackData(feedbackForm, null);
                recordService.saveRecord(request.getRemoteAddr(),4,feedbackId+"",null);
            }
            return ResultWrapper.success();
        }catch (DataIntegrityViolationException e){
            log.info("record表违反唯一约束，但是插入成功");
            return ResultWrapper.success();
        }  catch (Exception e) {
            log.info("FeedbackController.insetFeedbackData");
            return ResultWrapper.failure(e.getMessage());
        }
    }

    /**
     * 只针对登录过的用户
     * 查询FeedBack表中的记录
     *
     * @param token
     * @return
     */
    @ApiOperation("查询别人对自己发布软件的反馈")
    @RequestMapping(path = "/select", method = RequestMethod.POST)
    ResultWrapper selectFeedbackByToken(@RequestHeader("ANSWER_ACCESS_TOKEN") String token) {
        try {
            List<FeedbackDto> list = feedbackService.selectFeedBackDataByToken(token);
            return ResultWrapper.successWithData(list);
        } catch (Exception e) {
            log.info("FeedbackController.selectFeedbackByToken");
            return ResultWrapper.failure(e.getMessage());
        }

    }

    /**
     * 向Feed表中添加一条记录
     *
     * @param feedBackDataForm
     * @return
     */
    @ApiOperation("添加对网站的反馈")
    @RequestMapping( path = "/insertForWeb" , method = RequestMethod.POST)
    public ResultWrapper insertFeedForWeb(@Valid@RequestBody FeedBackDataForm feedBackDataForm,
                                          BindingResult bindingResult,
                                          HttpServletRequest request,
                                          @RequestHeader("ANSWER_ACCESS_TOKEN") String token){
        try {
            validateParams(bindingResult);
            int feedId = feedbackService.insertFeedForWeb(feedBackDataForm.getContent());
            String userId = redisRepository.selectAccessToken(token);
            recordService.saveRecord(request.getRemoteAddr(),5,feedId+"",userId);
            return ResultWrapper.success();
        }catch (DataIntegrityViolationException e){
            log.info("record表违反唯一约束，但是插入成功");
            return ResultWrapper.success();
        } catch(Exception e) {
            log.info("FeedbackController.insertFeedForWeb");
            return ResultWrapper.failure(e.getMessage());
        }

    }

}
